/*
 * Created on Dec 30, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


import ModuleBuilder.model.*;

/**
 * @author msoong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

/* ListDemo.java is a 1.4 application that requires no other files. */
public class ItemList extends JPanel
					  implements ListSelectionListener {
	private JList list;
	private DefaultListModel listModel;

	private static final String hireString = "Add";
	private static final String fireString = "Remove";
	private static final int _visibleRowCount=5;
	private JButton fireButton;
	private iFileSelectInput fileSelectDelegate;
	private iImagePreviewChangeListener imageChangeListener;
	// private HashMap allImages;

	public ItemList( iFileSelectInput in ) {
		super(new BorderLayout());

		fileSelectDelegate = in;
		listModel = new DefaultListModel();

		//Create the list and put it in a scroll pane.
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setSelectedIndex(0);
		list.addListSelectionListener(this);
		list.setVisibleRowCount( _visibleRowCount );
		JScrollPane listScrollPane = new JScrollPane(list);

		JButton hireButton = new JButton(hireString);
		AddListener hireListener = new AddListener(hireButton);
		hireButton.setActionCommand(hireString);
		hireButton.addActionListener(hireListener);
		hireButton.setEnabled(false);
		hireButton.setEnabled(true);

		fireButton = new JButton(fireString);
		fireButton.setActionCommand(fireString);
		fireButton.addActionListener(new RemoveListener());
		if ( listModel.getSize() > 0) {
			fireButton.setEnabled(true);
		} else {
			fireButton.setEnabled(false);
		}		

		String name;
		if ( listModel.getSize() > 0 ) {
			name = listModel.getElementAt(list.getSelectedIndex()).toString();
		}
		
		//Create a panel that uses BoxLayout.
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane,
										   BoxLayout.LINE_AXIS));
		buttonPane.add(fireButton);
		buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
		buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
		buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
		buttonPane.add(hireButton);
		buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

		add(listScrollPane, BorderLayout.CENTER);
		add(buttonPane, BorderLayout.PAGE_START);
		
		// allImages = new HashMap();
	}

	public void addImagePreview( iImagePreviewChangeListener in ) {
		imageChangeListener = in;
	}
	
	public JList getList() {
		return ( list );
	}

	public void addItemToList(String fn ) {
		listModel.addElement(fn);
		return;	
	}
	
	// fire = equivalent to the remove function
	//
	class RemoveListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//This method can be called only if
			//there's a valid selection
			String fn;
			int index = list.getSelectedIndex();
			
			if ( listModel.getSize() > 0) {
				// take this out of the model 
				fn=(String)listModel.getElementAt(index);
				// allImages.remove(fn);
				
				index = list.getSelectedIndex();
				list.setSelectedIndex(index-1);
				list.ensureIndexIsVisible(index-1);
				listModel.remove(index);
			}
			
			int size = listModel.getSize();

			if (size == 0) { //Nobody's left, disable firing.
				fireButton.setEnabled(false);

			} else { //Select an index.
				if (index == listModel.getSize()) {
					//removed item in last position
					index--;
				}

				list.setSelectedIndex(index);
				list.ensureIndexIsVisible(index);
			}
			
			// inform the imageChangeListener of the new image to be put up.			
			if (listModel.getSize() > 0) {
				fn=(String)listModel.getElementAt(index);
			} else {
				fn=null;
			}			
			imageChangeListener.imageChanged(fn);
			ImageCacheDelegate.removeImageElement(fn);
			// ImageElement el = (ImageElement)allImages.get(fn);
			// imageChangeListener.imageDeleted(el);
		}
	}

	//This listener is shared by the text field and the hire button.
	// Hire is equivalent to the add button
	class AddListener implements ActionListener, DocumentListener {
		private boolean alreadyEnabled = false;
		private JButton button;

		public AddListener(JButton button) {
			this.button = button;
		}

		//Required by ActionListener.
		public void actionPerformed(ActionEvent e) {
			
			String name = fileSelectDelegate.getSelectedFilePath();
			if (name.equals("") || alreadyInList(name)) {
				Toolkit.getDefaultToolkit().beep();
				return;
			}
			//If we just wanted to add to the end, we'd do this:
			int index = listModel.getSize()-1;
			if (index < 0) {
				index = 0;			
			}
			list.setSelectedIndex(index);
			list.ensureIndexIsVisible(index);
			listModel.addElement(name);						
			
			//Select the new item and make it visible.
			
			// inform the imageChangeListener of the new image to be put up.
			String fn=(String)listModel.getElementAt(index);
			imageChangeListener.imageChanged(fn);
			
			// Put the images into a imageElement and then pass it up to the 
			// manager
			//
			ImageElement el = ImageCacheDelegate.getImageElement(name);
			imageChangeListener.imageAdded(el);
			// allImages.put(fn, el);
		}

		//This method tests for string equality. You could certainly
		//get more sophisticated about the algorithm.  For example,
		//you might want to ignore white space and capitalization.
		protected boolean alreadyInList(String name) {
			return listModel.contains(name);
		}

		//Required by DocumentListener.
		public void insertUpdate(DocumentEvent e) {
			enableButton();
		}

		//Required by DocumentListener.
		public void removeUpdate(DocumentEvent e) {
			handleEmptyTextField(e);
		}

		//Required by DocumentListener.
		public void changedUpdate(DocumentEvent e) {
			if (!handleEmptyTextField(e)) {
				enableButton();
			}
		}

		private void enableButton() {
			if (!alreadyEnabled) {
				button.setEnabled(true);
			}
		}

		private boolean handleEmptyTextField(DocumentEvent e) {
			if (e.getDocument().getLength() <= 0) {
				button.setEnabled(false);
				alreadyEnabled = false;
				return true;
			}
			return false;
		}
	}

	//This method is required by ListSelectionListener.
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {
			int index = list.getSelectedIndex();
			if ( index == -1) {
			//No selection, disable fire button.
				fireButton.setEnabled(false);

			} else {
				//Selection, enable the fire button.
				fireButton.setEnabled(true);
				
				// inform the imageChangeListener of the new image to be put up.
				if ( index < listModel.getSize() ) {
					String fn=(String)listModel.getElementAt(index);
					imageChangeListener.imageChanged(fn);
				}
			}

		}
	}
	
	public int getItemCount() {
		return(listModel.getSize());
	}

	public String getItemName( int index ) {
		return ((String)listModel.get(index));
	}
	
	public ArrayList getAllImages() {
		// !!!
		return ( null );
	}
}

